package com.android.cl.coludtf.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.android.cl.coludtf.R;
import com.android.cl.coludtf.appcliation.MyAppcliation;
import com.android.cl.coludtf.model.Tenants;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class TenantAdapter extends BaseAdapter {
    LayoutInflater inflater;
    private List<Tenants> dataList;
    private static int LENGTH = 15;

    public TenantAdapter(List<Tenants> dataList) {

        super();// 继承自父类，用super可以调用父类的方法，实现父类方法中的功能，在添加你自己想实现的方法
        this.dataList = dataList;
        inflater = LayoutInflater.from(MyAppcliation.context);
    }

    @Override
    public int getCount() {
        return dataList.size();
    }

    @Override
    public Tenants getItem(int position) {
        return dataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;// = new ViewHolder(convertView);
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.gridview_item_tenant, null, false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.TxtTitleTenant.setText(getLengthString(getItem(position).getTenantName()));
        viewHolder.TxtTitleTenant.setTag(getItem(position).getId());
        if (getItem(position).getChecked()) {
            viewHolder.TxtBkgTenant.setVisibility(View.VISIBLE);
        } else {
            viewHolder.TxtBkgTenant.setVisibility(View.INVISIBLE);
        }
        return convertView;
    }

    static
    class ViewHolder {
        @BindView(R.id.Txt_Bkg_Tenant)
        TextView TxtBkgTenant;
        @BindView(R.id.Txt_Title_Tenant)
        TextView TxtTitleTenant;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    private String getLengthString(String Str) {
        String s = "";
        if (Str.length() > LENGTH) {
            s = Str.substring(0, LENGTH) + "...";
        } else {
            s = Str;
        }
        return s;
    }
}
